from db.dao import IndustrySectorFundsDao, StockInfoDao, IndustryInfoDao, StockMarketDao, StockFundsDao, \
    StockFundamentalDao, NewsDao, IndustryGrowthListDao, IndustryStockListDao, StockFundsHolderDao, MarketWorkDayDao
from db.models import StockInfo, IndustryInfo, IndustrySectorFund, StockMarket, StockFund, StockFundamental, News, \
    IndustryGrowthList, IndustryStockList, StockFundsHolder, MarketWorkDay
from spiders.items import SectorFundsItem, StockInfoItem, IndustryItem, StockMarketItem, StockFundsItem, \
    StockFundamentalItem, NewsItem, IndustryGrowthListItem, IndustryStockListItem, StockFundsHolderItem, \
    MarketWorkDayItem
from utils.util import ObjDictTool


class SpidersPipeline:
    def process_item(self, item, spider):
        if (isinstance(item, SectorFundsItem)):
            # 判断是否是SectorFundsItem
            sector_fund = IndustrySectorFund()
            ObjDictTool.to_obj(sector_fund, **item)
            setattr(sector_fund, 'stock_imported', 0)
            IndustrySectorFundsDao().save(sector_fund)
            # print(item)
        elif (isinstance(item, StockInfoItem)):
            '''股票信息'''
            # print("------------{}".format(item['stock_name']))
            stock_info = StockInfo()
            ObjDictTool.to_obj(stock_info, **item)
            StockInfoDao().saveOrUpdate(stock_info)
        elif (isinstance(item, IndustryItem)):
            '''板块信息'''
            industry_info = IndustryInfo()
            ObjDictTool.to_obj(industry_info, **item)
            IndustryInfoDao().saveOrUpdate(industry_info)
        elif (isinstance(item, StockMarketItem)):
            '''大盘'''
            market = StockMarket()
            ObjDictTool.to_obj(market, **item)
            StockMarketDao().saveOrUpdate(market)
        elif (isinstance(item, StockFundsItem)):
            '''股票涨幅榜'''
            fund = StockFund()
            ObjDictTool.to_obj(fund, **item)
            StockFundsDao().saveOrUpdate(fund)
        elif (isinstance(item, StockFundsHolderItem)):
            '''股票涨幅榜'''
            fund = StockFundsHolder()
            ObjDictTool.to_obj(fund, **item)
            StockFundsHolderDao().save(fund)
        elif (isinstance(item, StockFundamentalItem)):
            fundamental = StockFundamental()
            ObjDictTool.to_obj(fundamental, **item)
            StockFundamentalDao().save(fundamental)
        elif (isinstance(item, NewsItem)):
            news = News()
            ObjDictTool.to_obj(news, **item)
            NewsDao().saveOrUpdate(news)
        elif (isinstance(item, IndustryGrowthListItem)):
            industryGrowthList = IndustryGrowthList()
            ObjDictTool.to_obj(industryGrowthList, **item)
            IndustryGrowthListDao().saveOrUpdate(industryGrowthList)
        elif (isinstance(item, IndustryStockListItem)):
            stock_list = IndustryStockList()
            ObjDictTool.to_obj(stock_list, **item)
            IndustryStockListDao().saveOrUpdate(stock_list)
        elif (isinstance(item, MarketWorkDayItem)):
            marketWorkDay = MarketWorkDay()
            ObjDictTool.to_obj(marketWorkDay, **item)
            MarketWorkDayDao().saveOrUpdate(marketWorkDay)

        return item
